local PitBull4 = PitBull4
if not PitBull4 then
	error("PitBull4_VengeanceBar requires PitBull4")
end

local UnitBuff, UnitHealthMax = UnitBuff, UnitHealthMax
local tanks = {}

local InTankStance
do
	local class = select(2, UnitClass("player"))
	local tankStances = {
		PALADIN = true,
		DEATHKNIGHT = 1,
		DRUID = 1, -- 5
		WARRIOR = 2, -- 18
		MONK = 1, -- 23
	}
	local classTankStance = tankStances[class]

	function InTankStance()
		return classTankStance == true or classTankStance == GetShapeshiftForm()
	end
end


local GetVengeanceAP
do
	local floor = math.floor
	local vengeance = GetSpellInfo(93098)
	function GetVengeanceAP(unit)
		local ap = select(15, UnitBuff(unit or "player", vengeance)) or 0
		return floor(ap)
	end
end

local function GetVengeanceAPMax(unit)
	local hp = UnitHealthMax(unit or "player") or 0
	return hp -- * 0.3
end

--Allow for getting the AP value in LuaTexts
local PitBull4_LuaTexts = PitBull4:GetModule("LuaTexts", true)
if PitBull4_LuaTexts then
	PitBull4_LuaTexts.ScriptEnv.VengeanceAP = GetVengeanceAP
	PitBull4_LuaTexts.ScriptEnv.VengeanceAPMax = GetVengeanceAPMax
end

local PitBull4_VengeanceBar = PitBull4:NewModule("VengeanceBar", "AceEvent-3.0")
local L = LibStub("AceLocale-3.0"):GetLocale("PitBull4_VengeanceBar")

PitBull4_VengeanceBar:SetModuleType("bar")
PitBull4_VengeanceBar:SetName(L["Vengeance bar"])
PitBull4_VengeanceBar:SetDescription(L["Show a bar indicating the attack power gained from Vengeance."])
PitBull4_VengeanceBar:SetDefaults({
	check_stance = false,
	position = 4,
})

function PitBull4_VengeanceBar:OnEnable()
	self:RegisterEvent("PLAYER_ROLES_ASSIGNED")
	self:RegisterEvent("GROUP_ROSTER_UPDATE", "PLAYER_ROLES_ASSIGNED")
	self:RegisterEvent("PLAYER_SPECIALIZATION_CHANGED", "PLAYER_ROLES_ASSIGNED")
	self:PLAYER_ROLES_ASSIGNED()
end

function PitBull4_VengeanceBar:OnDisable()
	wipe(tanks)
	self:UpdateAll()
end

function PitBull4_VengeanceBar:GetValue(frame)
	local unit = frame.unit
	if tanks[unit] then -- or (tanks.player and UnitIsUnit("player", unit))
		if unit == "player" and self:GetLayoutDB(frame).check_stance and not InTankStance() then
			return
		end

		local cur, max = GetVengeanceAP(unit), GetVengeanceAPMax(unit)
		if cur == 0 then
			return 0
		elseif max > 0 then
			return GetVengeanceAP(unit) / max
		end
	end
end
function PitBull4_VengeanceBar:GetExampleValue(frame)
	if frame.unit == "player" then
		return 0.3
	end
end

function PitBull4_VengeanceBar:GetColor()
	local color = PitBull4.PowerColors.POWER_TYPE_HEAT
	if color then
		return color[1], color[2], color[3]
	end
end


function PitBull4_VengeanceBar:PLAYER_ROLES_ASSIGNED()
	self:UnregisterEvent("UNIT_AURA")
	self:UnregisterEvent("UNIT_MAXHEALTH")
	wipe(tanks)

	local spec = GetSpecialization()
	if spec and GetSpecializationRole(spec) == "TANK" then
		tanks.player = true
	end

	if IsInRaid() then
		for i=1, GetNumGroupMembers() do
			local unit = ("raid%d"):format(i)
			local role, _, combatRole = select(10, GetRaidRosterInfo(i))
			if (combatRole == "TANK" or role == "MAINTANK") then -- and not UnitIsUnit("player", unit)
				tanks[unit] = true
			end
		end
	elseif IsInGroup() then
		for i=1, GetNumSubgroupMembers() do
			local unit = ("party%d"):format(i)
			if UnitGroupRolesAssigned(unit) == "TANK" then
				tanks[unit] = true
			end
		end
	end

	if next(tanks) then
		self:RegisterEvent("UNIT_AURA")
		self:RegisterEvent("UNIT_MAXHEALTH")
	end

	self:UpdateAll()
end

function PitBull4_VengeanceBar:UNIT_AURA(_, unit)
	if tanks[unit] then
		self:UpdateForUnitID(unit)
	end
end

function PitBull4_VengeanceBar:UNIT_MAXHEALTH(_, unit)
	if tanks[unit] then
		local hp = UnitHealthMax(unit)
		if hp > 0 then
			self:UpdateForUnitID(unit)
		end
	end
end

PitBull4_VengeanceBar:SetLayoutOptionsFunction(function(self)
	return "check_stance", {
		type = "toggle",
		name = L["Hide in non-tank stance"],
		desc = L["Hide the vengeance bar for Player if not in a tanking stance."],
		get = function(info)
			return PitBull4.Options.GetLayoutDB(self).check_stance
		end,
		set = function(info, value)
			PitBull4.Options.GetLayoutDB(self).check_stance = value
			PitBull4_VengeanceBar:UpdateForUnitID("player")
		end,
	},
	"color_pvp_by_class", nil,
	"hostility_color", nil,
	"hostility_color_npcs", nil
end)

